/**
 * Created by Admin on 2017/12/29.
 */
define(function (require, exports, module) {
    var Paging = require("paging");
    var template = require("template");
    var loadingTpl = require("./tpl/loading.html");
    var paging = function (config) {
        this.config = {
            url: "",
            htmlContainer: "",
            pageContainer: "",
            tpl: "",
            data: ""
        }
        this.config = $.extend({}, this.config, config);
    }
    paging.prototype.init = function () {
        this.setPaging();
    }
    paging.prototype.setPaging = function () {
        var self = this;
        this.getAjax().done(function (data) {
            if (data.code == 0) {
                if (data.list.totalItemsCount >= 1) {
                    $(".nodata-title").hide();
                    $(self.config.htmlContainer).show();
                    $(self.config.pageContainer).show();
                        self.page = new Paging({
                            target: self.config.pageContainer,//渲染的元素
                            totalSize: data.list.totalItemsCount,//总共条数
                            pageSize: data.list.pageSize,//条数
                            pageShow: 8,//page-num显示多个
                            onChange: function () {
                                $(self.config.htmlContainer).html(template.compile(loadingTpl));
                                var currentPage = this.currentPage;
                                var pageSize = this.pageSize;
                                $("[name=pageSize]").val(pageSize);
                                $("[name=pageNumber]").val(currentPage);
                                $.get(self.config.url,$("#searchForm").serialize()).done(function (data) {
                                    var html = template.compile(self.config.tpl)({data: data})
                                    $(self.config.htmlContainer).html(html);
                                })
                            }
                        })
                    var html = template.compile(self.config.tpl)({data: data})
                    $(self.config.htmlContainer).html(html);
                } else {
                    $(".nodata-title").show();
                    $(self.config.htmlContainer).hide();
                    $(self.config.pageContainer).hide();
                }
                if ($(".patent-count").length > 0) {
                    $(".patent-count").html(data.list.totalItemsCount);
                }
            }
        })
    }


    paging.prototype.getAjax = function () {
        var self = this;
        // return $.get(self.config.url,{pageNum:1,pageSize:15});
        return $.get(self.config.url, self.config.data);
    }

    module.exports = paging;

})